<?php
/* type:param1:param2:param3 example: 1:1:100:200 means image with thumb
 *
 *
 */
$save_path =dirname(__FILE__).'/upload/';
//文件保存目录URL
$save_url ='/upload/';
$ext_arr = array(
	'image' => array('gif', 'jpg','png', 'jpeg', 'bmp'),
	'flash' => array('swf', 'flv'),
	'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
	'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
);
$save_path = realpath($save_path) . '/';
if(preg_match('/(\d+)\:(\d+):(\d+):(\d+)/U',$_SERVER['REQUEST_URI'],$matchs)){
//print_r($matchs);
if (!empty($_FILES)) {
	$tmp_name = $_FILES['Filedata']['tmp_name'];
	$file_name = $_FILES['Filedata']['name'];
	$file_size = $_FILES['Filedata']['size'];
	$dir_name='image';
	switch($matchs[1]){//设置文件目录
		case 1:
			$dir_name='image';
			break;
		case 2:
			$dir_name='file';
		break;	
		
		
	}	
	if (empty($ext_arr[$dir_name])) {		
		die;//目录名不正确
	}
	$temp_arr = explode(".", $file_name);
	$file_ext = array_pop($temp_arr);
	$file_ext = trim($file_ext);
	$file_ext = strtolower($file_ext);			
	if (in_array($file_ext, $ext_arr[$dir_name]) === false) {		
		echo $file_name."的扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。";
		die;
	}
if ($dir_name !== '') {
		$save_path .= $dir_name . "/";
		$save_url .= $dir_name . "/";
		if (!file_exists($save_path)) {
			mkdir($save_path);
		}
	}
	$ym = @date("Ym");
	$save_path .= $ym . "/";
	$save_url .= $ym . "/";
	if (!file_exists($save_path)) {
		mkdir($save_path);
		$op=fopen($save_path.'/index.html',"w");
		fwrite($op,':)');
	fclose($op);		
	}
	if ($matchs[1]==1&&!file_exists($save_path.'thumb/')) {
		mkdir($save_path.'thumb/');
		$op=fopen($save_path.'thumb/index.html',"w");
		fwrite($op,':)');
		fclose($op);
	}		
	//新文件名
	$new_file_name = @date("dHis") . rand(10000, 99999) . '.' . $file_ext;
	//移动文件
	$thumb_path=$save_path .'thumb/'. $new_file_name;
	$file_path = $save_path . $new_file_name;
	if (move_uploaded_file($tmp_name, $file_path) === false) {
		//$this->alert("上传文件失败。");
	}
		@chmod($file_path, 0644);
		if($matchs[1]==1&&$matchs[2]==1){
			$maxWidth=is_numeric($matchs[3])&&$matchs[3]>=200?$matchs[3]:200;
			$maxHeight=is_numeric($matchs[4])&&$matchs[4]>=50?$matchs[4]:50;
		require_once(dirname(__FILE__).'/controller/lib/Image.class.php');
		Image::thumb($file_path,$thumb_path,$type='',$maxWidth,$maxHeight,$interlace=true);
		}
	$file_url = $save_url . $new_file_name;
	echo $file_url;	
	}
}else if(preg_match('/(\d+)/U',$_SERVER['REQUEST_URI'],$matchs)){
	
}else{
die;	
}	
